package com.island.controller.provider;

import com.alibaba.fastjson.JSON;
import com.island.pojo.Provider;
import com.island.service.provider.ProviderService;
import com.island.utils.PageSupport;
import com.island.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jsp")
public class ProviderController {

    @Autowired
    private ProviderService providerService;
    @Autowired
    private PageSupport pageSupport;

    //1.获取供应商页的数据
    @RequestMapping("/queryprovider")
    public String queryProvider(HttpServletRequest request){
        String pageIndex = request.getParameter("pageIndex");
        String proCode=request.getParameter("queryProCode");
        String proName=request.getParameter("queryProName");
        //根据proCode或proName获取provider表中数据的总数
        int totalCount = providerService.getProviderCount(proCode,proName);
        //根据当前页数和相应表中数据的总数通过PageUtil获取相应的pageSupport对象
        pageSupport= PageUtil.getPageSupport(pageIndex,totalCount);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("proCode",proCode);
        map.put("proName",proName);
        //设置下一页页数和每页最大数据条数
        map.put("currentPageNo",(pageSupport.getCurrentPageNo()-1)* pageSupport.getPageSize());
        map.put("pageSize",pageSupport.getPageSize());
        //获取provider列表
        List<Provider> providers = providerService.queryProviderList(map);
        //传相应数据到前端
        request.setAttribute("providerList",providers);
        request.setAttribute("queryProCode",proCode);
        request.setAttribute("queryProName",proName);
        request.setAttribute("pageSupport",pageSupport);
        return "providerlist";
    }

    //2.删除provider数据
    @RequestMapping("/deleteprovider")
    @ResponseBody
    public String deleteProvider(String proid){
        HashMap<String,String> map = new HashMap<String, String>();
        int proId = Integer.parseInt(proid);
        //根据前端传入的proid获取相应provider对象
        Provider provider = providerService.queryProviderById(proId);
        if(provider!=null){
            try {
                providerService.deleteProvider(proId);
                System.out.println("您成功删除了"+provider);
                map.put("delResult","true");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("您没能删除"+provider);
                map.put("delResult","false");
            }
        }else {
            System.out.println("该供应商不存在");
            map.put("deResulte","notexist");
        }
        //返回json对象给ajax
        return JSON.toJSONString(map);

    }

    //3.获取相应proid的数据
    @RequestMapping("/viewprovider")
    public String viewProvider(String proid, Model model){

        int proId = Integer.parseInt(proid);
        //根据前端传入的proid获取相应provider对象
        Provider provider = providerService.queryProviderById(proId);
        model.addAttribute("provider",provider);
        return "providerview";
    }

    //4.根据前端输入的数据添加到provider表中
    @RequestMapping("/addprovider")
    public String addProvider(Provider provider){

        //System.out.println(provider);
        //设置创建provider的时间
        provider.setCreationDate(new Date());
        if(providerService.addProvider(provider)>0){
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败！");
        }
        //重定向到”/jsp/queryprovider“
        return "redirect:/jsp/queryprovider";
    }
    //5.跳转到providermodify页面
    @RequestMapping("/tomodifyprovider")
    public String toProviderModify(String proid,Model model){
        int proId = Integer.parseInt(proid);
        Provider provider = providerService.queryProviderById(proId);
        model.addAttribute("provider",provider);
        return "providermodify";
    }

    //6.根据前端输入的数据更新相应的数据
    @RequestMapping("/modifyprovider")
    public String modifyProvider(Provider provider){
        //System.out.println(provider);
        //设置修改provider表相应数据的时间
        provider.setModifyDate(new Date());
        try {
            providerService.updateProvider(provider);
            System.out.println("更新成功！");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("更新失败！");
        }
        //重定向到”/jsp/queryprovider"
        return "redirect:/jsp/queryprovider";
    }
}
